import {desDecrypt, genDesKey, rsaPubEncrypt} from "@/utils/EncryptUtils.js";
import request from "@/utils/request.js";
import {elMessageTip} from "@/utils/MessageTipUtils.js";
import {resolvePromise_data, resolvePromise_status} from "@/utils/ReturnUtils.js";
import useAccountStore from "@/store/modules/AccountStore.js";
import {post} from "@/utils/http.js";

/**
 * 获取用户全部信息
 */
export const getUserAllInfo = () => {
    const desKey = genDesKey()
    return request.get(
        '/user/all',
        {
            params: {
                account: rsaPubEncrypt(useAccountStore().account),
                desKey: rsaPubEncrypt(desKey)
            }
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            // elMessageTip(res)
            return resolvePromise_data(res)
        }
    )
}

export const updateAvatarAPI = (avatar) => {
    const desKey = genDesKey()
    return post(
        '/user/avatar',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            desKey: rsaPubEncrypt(desKey),
            avatar: rsaPubEncrypt(avatar),
            id: useAccountStore().user.id
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            elMessageTip(res)
            return resolvePromise_data(res)
        }
    )
}

export const updateAvatarBgAPI = (avatarBg) => {
    const desKey = genDesKey()
    return post(
        '/user/avatar/bg',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            desKey: rsaPubEncrypt(desKey),
            avatarBg: rsaPubEncrypt(avatarBg),
            id: useAccountStore().user.id
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            elMessageTip(res)
            return resolvePromise_data(res)
        }
    )
}

export const updateUsernameAPI = (username) => {
    const desKey = genDesKey()
    return post(
        '/user/username',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            desKey: rsaPubEncrypt(desKey),
            username: rsaPubEncrypt(username),
            id: useAccountStore().user.id
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            elMessageTip(res)
            return resolvePromise_data(res)
        }
    )
}

export const updatePwdAPI = (code, pwd) => {
    return post(
        '/user/pwd',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            id: useAccountStore().user.id,
            code: rsaPubEncrypt(code),
            pwd: rsaPubEncrypt(pwd)
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

export const updateUserInfoAPI = (phone, gender) => {
    return post(
        '/user/info',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            id: useAccountStore().user.id,
            phone: rsaPubEncrypt(phone),
            gender: rsaPubEncrypt(gender)
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}